package com.acmedcare.framework.exchange.center.endpoint.cqrs;

import com.acmedcare.framework.boot.snowflake.Snowflake;
import com.acmedcare.framework.exchange.center.entity.bean.TaskDetail;
import com.acmedcare.framework.exchange.center.entity.dto.TaskDetailCommandEvent;
import com.acmedcare.framework.exchange.center.repository.TaskDetailRepository;
import com.acmedcare.framework.starter.bus.AcmedcareEventListener;
import com.acmedcare.framework.starter.bus.AcmedcareEventPublisher;
import org.springframework.stereotype.Component;

@Component
public class TaskDetailCommandListener extends AcmedcareEventListener<TaskDetailCommandEvent> {

  private final TaskDetailRepository repository;
  private final Snowflake snowflake;

  public TaskDetailCommandListener(
      AcmedcareEventPublisher publisher,
      TaskDetailRepository repository, Snowflake snowflake) {
    super(publisher);
    this.repository = repository;
    this.snowflake = snowflake;
  }

  @Override
  public void onAcmedcareEvent(TaskDetailCommandEvent taskDetailCommandEvent){

    switch (taskDetailCommandEvent.getActionCommand()) {
      case Add:
        TaskDetail taskDetail = taskDetailCommandEvent.getData();
        taskDetail.setTaskDetailId(snowflake.nextId());
        repository.insert(taskDetail);
    }
  }
}
